package com.ruoyi.project.in.stockin.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;

import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.project.bas.basSupplier.service.IBasSupplierService;
import com.ruoyi.project.device.warehouse.service.IBasWarehouseService;
import com.ruoyi.project.in.stockin.domain.InStockVo;
import com.ruoyi.project.in.stockin.domain.Stockin;
import com.ruoyi.project.in.stockin.service.IStockinService;
import com.ruoyi.project.in.stockindetail.domain.StockinDetail;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 基础生产线 信息操作处理
 * 
 * @author sys
 * @date 2021-10-28
 */
@Controller
@RequestMapping("/in/stockin")
public class StockinController extends BaseController
{
    private String prefix = "in/stockin";
	
	@Autowired
	private IStockinService stockinService;

	@Autowired
	private IBasWarehouseService warehouseService;
	@Autowired
	private IBasSupplierService supplierService;

	/*******
	 * 跳转新建入库单页面
	 * @param mmap
	 * @return
	 */
	@GetMapping("/stockin")
	public String stockin(ModelMap mmap)
	{
	    return prefix + "/stockin";
	}

	/**
	 * 跳转单据管理页面
	 * @param mmap
	 * @return
	 */
	@GetMapping("/stockinManage")
	public String stockin1(ModelMap mmap)
	{
		return prefix + "/stockinManage";
	}
	/**
	 * 查询基础生产线列表
	 */
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Stockin stockin)
	{
		startPage();
            List<Stockin> list = stockinService.selectStockinList(stockin);
		return getDataTable(list);

	}

	/***
	 * 单据审核，确认，作废
	 * @param ids
	 * @param status
	 * @return
	 */
	@PostMapping("/audit")
	@ResponseBody
	public AjaxResult audit(String ids,String status)
	{
		String msg = stockinService.updatStatus(ids,status);
		return AjaxResult.success(msg);

	}

	/**
	 * 修改基础生产线
	 */
	@GetMapping("/edit/{stockinGuid}")
	public String edit(@PathVariable("stockinGuid") String stockinGuid, ModelMap mmap)
	{
		Stockin stockin = stockinService.selectStockinById(stockinGuid);
		mmap.put("stockin", stockin);
	    return prefix + "/edit";
	}

	/**
	 * 选择物品
	 */
	@GetMapping("/selectPro")
	public String selectPro(){
		return prefix + "/selectPro";
	}

	/**
	 * 选择供应商
	 */
	@GetMapping("/selectSupplier")
	public String selectSupplier(){
		return prefix + "/selectSupplier";
	}

	/**
	 * 新增入库主单
	 */
	@Log(title = "入库主单", businessType = BusinessType.INSERT)
	@PostMapping("/addSave")
	@ResponseBody
	public AjaxResult addSave(@RequestBody InStockVo inStockVo) {

		//验证是否有相同的物料属性和比例号
		List<StockinDetail> stockinDetailList = inStockVo.getStockinDetailList();
		for (int i = 0; i < stockinDetailList.size(); i++) {
			for (int j = i+1; j < stockinDetailList.size(); j++) {
				String productId = stockinDetailList.get(i).getProductId();
				String price = stockinDetailList.get(i).getPrice();//比例号
				String productId1 = stockinDetailList.get(j).getProductId();
				String price1 = stockinDetailList.get(j).getPrice();//比例号
				if (productId1 == productId && price1 == price){
					return new AjaxResult(AjaxResult.Type.ERROR,"存在物料、比例号相同入库信息，请合并入库！");
				}
			}
			if (i+1>=stockinDetailList.size()) {
				break;
			}
		}
		return stockinService.addSave(inStockVo);

	}

	@PostMapping("/handleBack")
	@ResponseBody
	public AjaxResult handleBack(@RequestBody Stockin stockin) {
		String classNowId = stockin.getClassNowId();
		String msg = "单据入出库方向不正确 请检查！";
		// 入库回传
		if ("1".equals(classNowId)){
			msg = stockinService.handleBack(stockin.getFID());
		}
		// 出库回传
		if ("2".equals(classNowId)){
			msg = stockinService.handleBackOut(stockin.getFID());
		}
		return AjaxResult.success(msg);
	}
}
